//ALL
//WMHREF = {};
$(window).load(function () {
    // Page Preloader
    $('#status').fadeOut();
    $('#preloader').delay(350).fadeOut(function () {
        $('body').delay(350).css({'overflow': 'visible'});
    });
});

function debounceFn(func, threshold, execAsap) {
    var timeout;
    return function debounced() {
        var obj = this, args = arguments;

        function delayed() {
            if (!execAsap)
                func.apply(obj, args);
            timeout = null;
        };
        if (timeout)
            clearTimeout(timeout);
        else if (execAsap)
            func.apply(obj, args);
        timeout = setTimeout(delayed, threshold || 100);
    };
}
//END ALL

$(document).ready(function () {
    // Toggle Left Menu
    $('.leftpanel .nav-parent > a').on('click', function (event) {
        event.stopPropagation();
        var parent = $(this).parent();
        var sub = parent.find('> ul');
        // Dropdown works only when leftpanel is not collapsed
        if (!$('body').hasClass('leftpanel-collapsed') || $(".leftpanel").hasClass('pmt-leftpanel')) {
            if (sub.is(':visible')) {
                sub.fadeOut(200, function () {
                    parent.removeClass('nav-active');
                });
            } else {
                closeVisibleSubMenu();
                parent.addClass('nav-active');
                sub.fadeIn(200);
            }
        }
    });
    $(window).click(function(){
        closeVisibleSubMenu();
    });
    function closeVisibleSubMenu() {
        $('.leftpanel .nav-parent').each(function () {
            var t = jQuery(this);
            if (t.hasClass('nav-active')) {
                t.find('> ul').fadeOut(200, function () {
                    t.removeClass('nav-active');
                });
            }
        });
    }

    //Menu Toggle leftpanel collapsed
    $('.menutoggle').click(function () {
        var body = $('body');
        if (!body.hasClass('leftpanel-collapsed')) {
            body.addClass('leftpanel-collapsed');
            $('.nav-mcd ul').attr('style', '');
        } else {
            body.removeClass('leftpanel-collapsed');
        }
    });

    //Add class a mouse pointer hover over leftpanel nav
    if (!$(".leftpanel").hasClass('pmt-leftpanel')) {
        $('.nav-mcd > li').hover(function () {
            $(this).addClass('nav-hover');
        }, function () {
            $(this).removeClass('nav-hover');
        });
    }

    //stopPropagation onClick tabCheckbx & task
    $('.tab-filter').click(function (e) {
        e.stopPropagation();
    });
    $('#todoBtn').click(function () {
        $('#todoList').mixItUp({
            animation: {
                duration: 400,
                effects: 'fade translateZ(-360px) stagger(34ms)',
                easing: 'ease'
            },
            selectors: {
                target: '.mix',
                filter: '.filter'
            }
        });
    });
   /* $('#quickviewTodoList').mixItUp({
        animation: {
            duration: 400,
            effects: 'fade translateZ(-360px) stagger(34ms)',
            easing: 'ease'
        },
        selectors: {
            target: '.mix',
            filter: '.filter'
        }
    });*/

    //select form
    $('.search-input .dropdown-menu li').on('click', function () {
        var val = $(this).text();
        $(this).parents('.search-input').find('.input-value').html(val);
    });


//MCD_PORTAL_PORTAL.HTML
    if ($("#carousel-pmt-generic").length > 0) {
        $("#carousel-pmt-generic").swipe({
            swipeLeft: function () {
                $(this).carousel('next');
            },
            swipeRight: function () {
                $(this).carousel('prev');
            }
        });
    }
    mediaFn();
});
function mediaFn() {
    window.onload = window.onresize = debounceFn(function () {
        var win_w = $(window).width() + 17;
        var screen_w = screen.width;
//        console.log(win_w);
        var $navThemeA = $("#navThemeA");
        var $navThemeABtn = $("#navThemeABtn");
        var $pmtLeftpanelNavToBtnBox = $("#pmtLeftpanelNavToBtnBox");
        var $pmtLeftpanelNav = $("#pmtLeftpanelNav");
        if (win_w > 992 && win_w <= 1120) {
            $(".lg-nav", $navThemeA).prependTo($navThemeABtn);
            $(".sm-nav", $navThemeABtn).appendTo($navThemeA);
            $(".md-nav", $navThemeABtn).appendTo($navThemeA);
        } else if (win_w > 768 && win_w <= 992) {
            $(".lg-nav", $navThemeA).prependTo($navThemeABtn);
            $(".md-nav", $navThemeA).prependTo($navThemeABtn);
            $(".sm-nav", $navThemeABtn).appendTo($navThemeA);
        } else if (win_w > 475 && win_w <= 768) {
            $(".lg-nav", $navThemeA).prependTo($navThemeABtn);
            $(".md-nav", $navThemeA).prependTo($navThemeABtn);
            $(".sm-nav", $navThemeA).prependTo($navThemeABtn);
        } else if (win_w > 1120) {
            $(".sm-nav", $navThemeABtn).appendTo($navThemeA);
            $(".md-nav", $navThemeABtn).appendTo($navThemeA);
            $(".lg-nav", $navThemeABtn).appendTo($navThemeA);
        } else if (win_w <= 475) {
            $(".lg-nav", $navThemeA).prependTo($navThemeABtn);
            $(".md-nav", $navThemeA).prependTo($navThemeABtn);
            $(".sm-nav", $navThemeA).prependTo($navThemeABtn);
        }
        if (screen_w <= 475) {
            $("li", $pmtLeftpanelNav).appendTo($pmtLeftpanelNavToBtnBox);
        } else {
            $("li", $pmtLeftpanelNavToBtnBox).appendTo($pmtLeftpanelNav);
        }
    }, 300, false);
}
//END MCD_PORTAL_PORTAL.HTML


!function () {
    $(function () {
        $(".panel-wm-b").find(".panel-btn-close").click(function () {
            var $this = $(this);
            var $parent = $this.parent().parent();
            if ($parent.hasClass("panel-is-close")) {
                $parent.find(".panel-body").slideDown(200, function () {
                    $parent.removeClass("panel-is-close");
                });
            } else {
                $parent.find(".panel-body").slideUp(200, function () {
                    $parent.addClass("panel-is-close");
                });
            }
        });

        //William 2015-3-3-11:17 dcl.html List点击事件
        $(".list-group-a").find("li").click(function () {
            var $this = $(this);
            $this.addClass("active").siblings().removeClass("active");
        });
        //END

        // William 2015-3-3-11:20 left-box & left-box-has等高
        function panelHigh() {
            $(".left-box .panel-wm-d").css({"min-height": $(".left-box-has").height()});
            $(".mainpanel-con").css({"min-height": $(".left-box").height()});
        }

        panelHigh();
        $(window).resize(function () {
            panelHigh();
        });
        //END

    });
}();

/*Stephen*/
//$(function(){
//    $('.mainpanel-con .contentpanel').hide();
//var mainpanelConLoaderSetTime = setTimeout(function(){
//    // Page Preloader
//    $('#mainpanelConLoaderStatus').fadeOut();
//    $('#mainpanelConLoader').delay(1350).fadeOut(function () {
//        $('.mainpanel-con .contentpanel').fadeIn(300);
//    });
//    clearTimeout(mainpanelConLoaderSetTime);
//},3000);
//});
/*END Stephen*/

$(window).scroll(function () {

});
/*Pin*/
(function ($) {
    "use strict";
    $.fn.pin = function (options) {
        var scrollY = 0, elements = [], disabled = false, $window = $(window);
        options = options || {};
        var recalculateLimits = function () {
            for (var i = 0, len = elements.length; i < len; i++) {
                var $this = elements[i];
                if (options.minWidth && $window.width() <= options.minWidth) {
                    if ($this.parent().is(".pin-wrapper")) {
                        $this.unwrap();
                    }
                    $this.css({width: "", left: "", top: "", position: ""});
                    if (options.activeClass) {
                        $this.removeClass(options.activeClass);
                    }
                    disabled = true;
                    continue;
                } else {
                    disabled = false;
                }
                var $container = options.containerSelector ? $this.closest(options.containerSelector) : $(document.body);
                var offset = $this.offset();
                var containerOffset = $container.offset();
                var parentOffset = $this.offsetParent().offset();
                if (!$this.parent().is(".pin-wrapper")) {
                    $this.wrap("<div class='pin-wrapper'>");
                }
                var pad = $.extend({
                    top: 0,
                    bottom: 0
                }, options.padding || {});
                $this.data("pin", {
                    pad: pad,
                    from: (options.containerSelector ? containerOffset.top : offset.top) - pad.top,
                    to: containerOffset.top + $container.height() - $this.outerHeight() - pad.bottom,
                    end: containerOffset.top + $container.height(),
                    parentTop: parentOffset.top
                });
//                $this.css({width: $this.outerWidth()});
                $this.parent().css("height", $this.outerHeight());
            }
        };
        var onScroll = function () {
            if (disabled) {
                return;
            }
            scrollY = $window.scrollTop();
            var elmts = [];
            for (var i = 0, len = elements.length; i < len; i++) {
                var $this = $(elements[i]),
                    data = $this.data("pin");
                if (!data) { // Removed element
                    continue;
                }
                elmts.push($this);
                var from = data.from - data.pad.bottom,
                    to = data.to - data.pad.top;
                if (from + $this.outerHeight() > data.end) {
                    $this.css('position', '');
                    continue;
                }
                if (from < scrollY && to > scrollY) {
                    !($this.css("position") == "fixed") && $this.css({
                        left: $this.offset().left,
                        top: data.pad.top
                    }).css("position", "fixed");
                    if (options.activeClass) {
                        $this.addClass(options.activeClass);
                    }
                } else if (scrollY >= to) {
                    $this.css({
                        left: "",
                        top: to - data.parentTop + data.pad.top
                    }).css("position", "absolute");
                    if (options.activeClass) {
                        $this.addClass(options.activeClass);
                    }
                } else {
                    $this.css({position: "", top: "", left: ""});
                    if (options.activeClass) {
                        $this.removeClass(options.activeClass);
                    }
                }
            }
            elements = elmts;
        };
        var update = function () {
            recalculateLimits();
            onScroll();
        };
        this.each(function () {
            var $this = $(this),
                data = $(this).data('pin') || {};
            if (data && data.update) {
                return;
            }
            elements.push($this);
            $("img", this).one("load", recalculateLimits);
            data.update = update;
            $(this).data('pin', data);
        });
        $window.scroll(onScroll);
        $window.resize(function () {
            recalculateLimits();
        });
        recalculateLimits();
        $window.load(update);
        return this;
    };
})(jQuery);
/*END Pin*/
$("#formButtonGroup").pin({containerSelector: ".contentpanel", minWidth: 500, padding: {top: "37"}});
$(".full-box.mobile-scrll").pin({containerSelector: ".mainpanel", minWidth: 500, activeClass: "pin-fixed-class"});
//$(".left-agent-fixed").pin({ minWidth: 500, activeClass: "pin-fixed-class"});
//批量选人
(function ($) {
    "dual-drag-drop";
    $.fn.dualDragDrop = function (options) {
        var elements = [], $window = $(window);
//        options = options || {};
        var init = function () {
            for (var i = 0, len = elements.length; i < len; i++) {
                var $this = elements[i];
                $this.on('click', '.list-group .list-group-item', function () {
                    $(this).toggleClass('active');
                });
                $this.find('.list-arrows button').click(function () {
                    var $button = $(this), actives = '';
                    if ($button.hasClass('move-left')) {
                        actives = $this.find('.list-right ul li.active');
                        actives.clone().appendTo($this.find('.list-left ul'));
                        actives.remove();
                    } else if ($button.hasClass('move-right')) {
                        actives = $this.find('.list-left ul li.active');
                        actives.clone().appendTo($this.find('.list-right ul'));
                        actives.remove();
                    } else if ($button.hasClass('move-clear')) {
                        actives = $('.list-group-item', $this);
                        actives.removeClass('active');
                    } else if ($button.hasClass('move-all-left')) {
                        actives = $this.find('.list-right ul li');
                        actives.clone().appendTo($this.find('.list-left ul'));
                        actives.remove();
                    } else if ($button.hasClass('move-all-right')) {
                        actives = $this.find('.list-left ul li');
                        actives.clone().appendTo($this.find('.list-right ul'));
                        actives.remove();
                    }
                });
                $this.find('.dual-list .selector').click(function () {
                    var $checkBox = $(this);
                    if (!$checkBox.hasClass('selected')) {
                        $checkBox.addClass('selected').closest('.dual-list').find('ul li:not(.active)').addClass('active');
                        $checkBox.children('i').removeClass('glyphicon-unchecked').addClass('glyphicon-check');
                    } else {
                        $checkBox.removeClass('selected').closest('.dual-list').find('ul li.active').removeClass('active');
                        $checkBox.children('i').removeClass('glyphicon-check').addClass('glyphicon-unchecked');
                    }
                });
                $this.find('[name="SearchDualList"]').keyup(function (e) {
                    var code = e.keyCode || e.which;
                    if (code == '9') return;
                    if (code == '27') $(this).val(null);
                    var $rows = $(this).closest('.dual-list').find('.list-group li');
                    var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();
                    $rows.show().filter(function () {
                        var text = $(this).text().replace(/\s+/g, ' ').toLowerCase();
                        return !~text.indexOf(val);
                    }).hide();
                });
            }
        };
        this.each(function () {
            var $this = $(this);
            elements.push($this);
        });
        $window.load(init());
        return this;
    };
})(jQuery);
//批量抄送
(function ($) {
    "tagsinput-drag-drop";
    $.fn.tagsinputDragDrop = function (options) {
        var elements = [], $window = $(window);
//        options = options || {};
        var init = function () {
            for (var i = 0, len = elements.length; i < len; i++) {
                var $this = elements[i], id = 0;
                $this.find(".tagsinput-down-list .list-group-item").each(function () {
                    $(this).attr("data-value-id", ++id);
                });
                $this.on('click', '.tagsinput-down-list .list-group-item', function (e) {
                    e.stopPropagation();
                    var $_this = $(this);
                    if (!$_this.hasClass('active')) {
                        $('<span class="btn btn-mcd btn-smer bg-f5f5f5" data-value-id="' + $_this.attr("data-value-id") + '">' + $_this.text() + '<i class="fa fa-times pull-right"></i></span>').appendTo($this.find('.tagsinput-list'));
                        $_this.addClass('active');
                    } else {
                        $_this.removeClass('active');
                        $(".tagsinput-list [data-value-id=" + $_this.attr("data-value-id") + "]", $this).remove();
                    }
                });
                $this.on('click', '.tagsinput-list span.btn', function (e) {
                    e.stopPropagation();
                    var $_this = $(this);
                    $_this.remove();
                    $(".tagsinput-down-list [data-value-id=" + $_this.attr("data-value-id") + "]", $this).removeClass('active');
                });
                $this.on('click', '.tagsinput-add', function (e) {
                    e.stopPropagation();
                    var $_this = $(this);
                    if ($this.find(".tagsinput-down-list").is(":hidden")) {
                        $this.find(".tagsinput-down-list").show();
                        $_this.find("i").removeClass('fa-plus').addClass('fa-minus');
                    } else {
                        $this.find(".tagsinput-down-list").hide();
                        $_this.find("i").removeClass('fa-minus').addClass('fa-plus');
                    }
                });
                $("body").click(function () {
                    $this.find(".tagsinput-down-list").hide();
                    $this.find(".tagsinput-add i").removeClass('fa-minus').addClass('fa-plus');
                });
                $this.find('.tagsinput-down-list .selector').click(function () {
                    var $checkBox = $(this);
                    if (!$checkBox.hasClass('selected')) {
                        $checkBox.addClass('selected').closest('.tagsinput-down-list').find('ul li:not(.active)').addClass('active');
                        $checkBox.children('i').removeClass('glyphicon-unchecked').addClass('glyphicon-check');
                    } else {
                        $checkBox.removeClass('selected').closest('.tagsinput-down-list').find('ul li.active').removeClass('active');
                        $checkBox.children('i').removeClass('glyphicon-check').addClass('glyphicon-unchecked');
                    }
                });
                $this.find('[name="SearchDualList"]').keyup(function (e) {
                    var code = e.keyCode || e.which;
                    if (code == '9') return;
                    if (code == '27') $(this).val(null);
                    var $rows = $(this).closest('.tagsinput-down-list').find('.list-group li');
                    var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();
                    $rows.show().filter(function () {
                        var text = $(this).text().replace(/\s+/g, ' ').toLowerCase();
                        return !~text.indexOf(val);
                    }).hide();
                }).click(function (e) {
                    e.stopPropagation();
                });
            }
        };
        this.each(function () {
            var $this = $(this);
            elements.push($this);
        });
        $window.load(init());
        return this;
    };
})(jQuery);
$(function () {
    $("#dualDragDrop_a").dualDragDrop();
    $("#dualDragDrop_b").tagsinputDragDrop();
});
//END 批量选人
//待处理
$(function () {
    $(".search-btn-b .active-group > .btn").click(function () {
        $(this).addClass("active").siblings().removeClass("active");
    });
});
//END 待处理
//btn-select 自定义Select风格
$(function () {
    function btnSelectFn(_this) {
        var $this = _this;
        var $btnSelect = $this.closest(".btn-select");
        var $curSelect = $btnSelect.find(".cur-select");
        var $oSelect = $btnSelect.find("select");
        var text = $oSelect[0].options[$oSelect[0].selectedIndex].text;
        $curSelect.text(text);
    }
    $("body").delegate(".btn-select>select", "change", function () {
        btnSelectFn($(this));
    });
    $(".btn-select").each(function () {
        btnSelectFn($(this));
    });
});
//END btn-select 自定义Select风格
//选择抄送人组件
$(function () {
    if ($(".tagsinput-group").length > 0) {
        $(".tagsinput-group").tagsinput({width: 'auto', height: 'auto', defaultText: "在此输入！"});
        $("body").delegate(".tagsinput-group-allremove", "click", function () {
            var $this = $(this);
            $this.closest(".grid-forms-p").find('.tagsinput-group').tagsinput('removeAll');
        });
    }
});
//END 选择抄送人组件
//点击回复按钮下拉回复模块
$(function () {
    $("body").delegate(".list-group-Reply-show", "click", function () {
        var $this = $(this);
        var $listGroupReply = $this.closest(".list-group-item").next(".list-group-Reply");
        var $listGroupReplyShow = $this.closest(".list-group").find(".list-group-Reply-show");
        if ($listGroupReply.is(":hidden")) {
            $listGroupReply.show().siblings(".list-group-Reply").hide();
            $listGroupReplyShow.find(".ts-opened").show().siblings().hide();
            $this.find(".ts-opened").hide().siblings().show();
        } else {
            $listGroupReply.hide();
            $this.find(".ts-opened").show().siblings().hide();
        }
    });
});
//END 点击回复按钮下拉回复模块
//panel-wm-b表格TR奇数添加样式及ASC&DESC排序事件
$(function () {
    $(".tr-even tr:even").addClass("tr-even");
    $("body").delegate(".panel-wm-b thead th", "click", function () {
        var $this = $(this);
        var $partent = $(this).closest(".panel-wm-b");
        if ($this.hasClass("sorting")) {
            $partent.find(".sorting-asc,.sorting-desc").removeClass("sorting-asc").removeClass("sorting-desc").addClass("sorting");
            $this.removeClass("sorting").addClass("sorting-asc");
        } else if ($this.hasClass("sorting-asc")) {
            $this.removeClass("sorting-asc").addClass("sorting-desc");
        } else if ($this.hasClass("sorting-desc")) {
            $this.removeClass("sorting-desc").addClass("sorting-asc");
        } else {
            return false;
        }
    });
});
//END panel-wm-b表格TR奇数添加样式
//操作台 状态更新下 radio效果
$(function(){
    $("#pelineListStatusRedio label").click(function(){
        var $this = $(this);
        var $thisRadio = $this.find("input");
        if($thisRadio[0].name=="Remain"){
            $("#pelineListStatusWatchBox,#pelineListStatusRedundantBox").hide();
        }else if($thisRadio[0].name=="Watch"){
            $("#pelineListStatusWatchBox").show();
            $("#pelineListStatusRedundantBox").hide();
        }else if($thisRadio[0].name=="Redundant"){
            $("#pelineListStatusWatchBox").hide();
            $("#pelineListStatusRedundantBox").show();
        }
    });
});
//END 操作台 状态更新下 radio效果
//radio btn list效果重写。原因不兼容ko
$(function(){
    function radioGroupSelectFn(_this) {
        var $this = _this;
        var $radioSelect = $this.closest(".radio-buttons-group");
        if($radioSelect.hasClass("disabled")){
            return false;
        }
        var $olabel = $this.find("input[type=radio]");
        if($this.hasClass("active")){
            return false;
        }else{
           $radioSelect.find("input[type=radio]").attr("checked",false);
           $olabel.attr("checked",true);$olabel.change();
           $this.addClass("active").siblings().removeClass("active");
        }
    }

    $("body").delegate(".radio-buttons-group>label", "click", function () {
        radioGroupSelectFn($(this));
    });
    $(".radio-buttons-group>label").each(function () {
//        radioGroupSelectFn($(this));
    });
});
//END radio btn list效果重写。原因不兼容ko
//订阅弹窗 checkbox All 点击事件
$(function(){
    $(".checkbox-all-btn input[type=checkbox]").change(function(){
        var $this = $(this);
        var $checkboxAllBox= $(".checkbox-all-box input[type=checkbox]");
        if($this.prop("checked")){
            $checkboxAllBox.prop("checked",true);
        }else {
            $checkboxAllBox.prop("checked",false);
        }
    });
});
//END 订阅弹窗 checkbox All 点击事件
//双击取消选择出现蓝色选中文本背景
$(function(){
    $(".dblselectstart-none").each(function(){
        var $this = $(this);
        $this[0].onselectstart= function(){
            return false;
        }
    });
//    document.onselectstart= function(){
//        event.returnValue=false
//    }
});
//END 双击取消选择出现蓝色选中文本背景
//左下角代理切换
$(function(){
    var $win = $(window);
    $("#agentImgBoxBtn").click(function(e){
        e.stopPropagation();
        $("#agentImgBoxSelect").fadeIn("300");
    });
    $("#agentImgBoxSelect .agent-img-box").click(function(e){
        var $this= $(this);
        $this.addClass("active").siblings().removeClass("active");
        var $agentImgBoxBtn = $("#agentImgBoxBtn");
        var $agentImgBoxSelectActive = $("#agentImgBoxSelect").find(".active");
        $agentImgBoxBtn.empty().append($agentImgBoxSelectActive.children().clone());
    });
    window.onload= window.onresize= function(){
        var n = $("#agentImgBoxSelect").find(".agent-img-box").length;
        $("#agentImgBoxSelect").width(n*64+n*10+10);
    }
    $(window).click(function(){
        $("#agentImgBoxSelect").fadeOut("300");
    });
});
//END 左下角代理切换
//左测Bar 顶部展开收起事件
$(function(){
    var $pmtLeftpanelNav = $("#pmtLeftpanelNav");
    $(".pmt-leftpanel-btn").click(function(){
        $pmtLeftpanelNav.fadeToggle();
    });
});
//END 左测Bar 顶部展开收起事件